package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.util.StringUtils;
import com.spa.infrastructure.repository.condition.MassageServiceFlowCondition;
import com.spa.infrastructure.repository.mapper.MassageServiceFlowMapper;
import com.spa.infrastructure.repository.po.MassageServiceFlowPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class MassageServiceFlowRepositoryService extends ServiceImpl<MassageServiceFlowMapper, MassageServiceFlowPO> {
    @Autowired
   private MassageServiceFlowMapper massageServiceFlowMapper;

    public Long add(MassageServiceFlowPO massageServiceFlowPO) {
        massageServiceFlowMapper.insert(massageServiceFlowPO);
        return massageServiceFlowPO.getId();
    }

    public void delete(MassageServiceFlowCondition condition) {
        massageServiceFlowMapper.delete(wrapper(condition));
    }
    public void updateByCondition(MassageServiceFlowPO po) {
        update(null, updateWrapper(po));
    }

    public List<MassageServiceFlowPO> list(MassageServiceFlowCondition condition) {
        return list(wrapper(condition));
    }
    private LambdaUpdateWrapper<MassageServiceFlowPO> updateWrapper(MassageServiceFlowPO po) {
        LambdaUpdateWrapper<MassageServiceFlowPO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ObjectUtil.isNotNull(po.getServiceId()), MassageServiceFlowPO::getServiceId, po.getServiceId());
        updateWrapper.set(StringUtils.isNotEmpty(po.getContent()), MassageServiceFlowPO::getContent, po.getContent());
        updateWrapper.set(ObjectUtil.isNotNull(po.getDuration()), MassageServiceFlowPO::getDuration, po.getDuration());
        updateWrapper.set(StringUtils.isNotEmpty(po.getImg()), MassageServiceFlowPO::getImg, po.getImg());
        return updateWrapper;
    }

    private LambdaQueryWrapper<MassageServiceFlowPO> wrapper(MassageServiceFlowCondition condition) {
        LambdaQueryWrapper<MassageServiceFlowPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotNull(condition.getServiceId()), MassageServiceFlowPO::getServiceId, condition.getServiceId());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getServiceIds()), MassageServiceFlowPO::getServiceId, condition.getServiceIds());
        return wrapper;
    }
}
